﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shorten.Core;
using Shorten.Core.Helpers;
using Shorten.Core.Models;
using Shorten.Core.Providers;
namespace Shorten.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        public ActionResult Index(string format)
        {
            if (!String.IsNullOrEmpty(format))
            {
                if (format == "xml")
                {
                    ShortenHelper.GetShortenUrlXml();
                }
                else if (format == "json")
                {
                    ShortenHelper.GetShortenUrlJson();
                }
                return null;
            }
            else
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Redirect(string Id)
        {
	        //TODO: Add code here to allow for querystring format paramters to return as xml or json
            string LongUrl = ShortenManagement.GetLongUrlByShortUrl(Id);
            LogItem.AddToLog(new ShortenItems(Id, LongUrl), LogItem.ActionType.Get, null);
            Response.Redirect(LongUrl);
            // return null is only because ActionResult need to return value, but return null is never fired.
            return null;
        }
     
    }
}
